package logic

import (
	"ck/ckrpc/ckrpc"
	"ck/ckrpc/internal/svc"
	"ck/models/mysql"
	"context"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopAddLogic {
	return &ShopAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopAddLogic) ShopAdd(in *ckrpc.ShopAddRequest) (*ckrpc.ShopAddResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Debug().Where("id=?", in.UserId).Find(&user)
	if user.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	shop := mysql.Shop{
		ShopName: in.ShopName,
		Address:  in.Address,
		UserId:   int(in.UserId),
	}
	mysql.DB.Debug().Create(&shop)

	return &ckrpc.ShopAddResponse{
		ShopId: int64(shop.ID),
	}, nil
}
